home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-02-27 | 3.0 KB | 124 lines | [TEXT/CWIE] |
- /* SK8 © 1997 Apple Computer, Inc.
- This code is protected under the current SK8 License
- See http://sk8.research.apple.com/ for more information
- Apple Research Laboratories
- */
-
-
- import java.io.*;
-
-
- public class stream extends Object {
- private file mFile;
- private String mIfExists; //"error", "rename","overwrite" or "append"
- private String mIfDoesNotExist; //"error" or "create"
- protected String mDirection; //"input", "ouput", or "closed"
-
-
- //private FileInputStream mInFileStream;
- //private BufferedInputStream mInBufferedStream;
- protected DataInputStream mInDataStream;
-
- //private FileOutputStream mOutFileStream;
- //private BufferedOutputStream mOutBufferedStream;
- protected DataOutputStream mOutDataStream;
-
- public String ifexists(){
- return mIfExists;
- }
- public void set_ifexists(String s){
- if (s.equals("error") ||
- s.equals("rename") ||
- s.equals("overwrite") ||
- s.equals("append")) {
- mIfExists = s;
- }
- }
-
- public String ifdoesnotexist(){
- return mIfDoesNotExist;
- }
- public void set_ifdoesnotexist(String s){
- if (s.equals("error") ||
- s.equals("create")) {
- mIfDoesNotExist = s;
- }
- }
-
-
- public String direction (){
- return mDirection;
- }
-
- public boolean set_direction (String inVal){
- inVal = inVal.toLowerCase();
- if (!(inVal.equals("input")) && !(inVal.equals("output")) && !(inVal.equals("closed"))){
- return false;
- }
-
- if (inVal.equals("closed"))
- try { this.close(); } catch (IOException e) {}
- else
- mDirection = inVal;
- return true;
- }
-
- public file file(){
- return mFile;
- }
-
- public boolean set_file (file f){
- mFile = f;
- return true;
- }
-
- public void close() throws IOException {
- if ((mDirection.equals( "input")) && (mInDataStream != null)){
- mInDataStream.close();
-
- } else if ((mDirection.equals("output")) && (mOutDataStream != null)){
- mOutDataStream.close();
- }
-
- mDirection = "closed";
- }
-
- public void flush() throws IOException {
- if ((mDirection.equals("output")) && (mOutDataStream != null)){
- mOutDataStream.flush();
- }
- }
-
-
-
-
-
- protected void setupStreams() throws IOException {
- if (mFile != null) {
- if (mDirection.equals("input")) {
- if (mFile.fileexists()) {
- String origName = mFile.physicalname();
- if (mIfExists.equals("error"))
- throw new IOException ("Writing to file that already exists");
- else if (mIfExists.equals("rename")) {
- for(int i = 1; mFile.fileexists(); i++)
- mFile.setname(origName + " " + i);
- }
- } else {
- if (mIfExists.equals("error"))
- throw new IOException ("Writing to file that dosen't exist");
- };
-
- FileInputStream FIS = new FileInputStream(mFile.file());
- BufferedInputStream BIS = new BufferedInputStream(FIS);
- mInDataStream = new DataInputStream(BIS);
- } else if (mDirection.equals("output")){
- FileOutputStream FOS = new FileOutputStream(mFile.file());
- BufferedOutputStream BOS = new BufferedOutputStream(FOS);
- mOutDataStream = new DataOutputStream(BOS);
- }
- }
- }
-
-
- }